package tq.com.tqteacher.mine.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.mine.adapter.TQClassIntrAdapter;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.net.TQNetInterface;

public class TQClassIntrFragment extends BaseRecyclerFragment {
    private TQClassIntrAdapter mTQClassIntrAdapter;
    private ArrayList<SubjectBrif> mSubjectBrifs  = new ArrayList<>();

    @Override
    protected void initRecycleView(RecyclerView pRecyclerView) {
        super.initRecycleView(pRecyclerView);

        if(mTQClassIntrAdapter == null){
            mTQClassIntrAdapter = new TQClassIntrAdapter(getContext());
            mTQClassIntrAdapter.setClickListener(mIClickListener);
        }

        pRecyclerView.setAdapter(mTQClassIntrAdapter);
        pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

        getData();
    }

    private void getData(){
        TQNetInterface.getClsBrif(TQLogin.getCurrent().getUserInfo().getClzid(), new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQClassIntrFragment.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                if(pNetResult.getData() == null) return;
                JSONObject lJSONObject = pNetResult.getData().optJSONObject("class");
                if(lJSONObject == null) return;

                JSONArray lArray = lJSONObject.optJSONArray("brief");
                if(lArray == null) return;

                for (int i = 0; i < lArray.length(); i++){
                    try {
                        JSONObject lObject = lArray.getJSONObject(i);

                        SubjectBrif lSubjectBrif = new SubjectBrif();
                        lSubjectBrif.fromJson(lObject);

                        mSubjectBrifs.add(lSubjectBrif);
                    }catch (JSONException pE){
                        pE.printStackTrace();
                    }
                }

                if(mSubjectBrifs.size() > 0){
                    setDefault();
                }
            }
        });
    }

    private void setDefault(){
        mTQClassIntrAdapter.updateInfo(mSubjectBrifs);

    }
    public class SubjectBrif{
        public int subject;
        public ArrayList<TypeBrif> mTypeBrifs = new ArrayList<>();

        public void fromJson(JSONObject pObject){
            if(pObject == null) return;

            subject = pObject.optInt("subject", TQSystemDefine.SUBJECT_yuwen);
            JSONArray lArray = pObject.optJSONArray("cate");
            if(lArray == null || lArray.length() == 0) return;

            for (int i = 0; i < lArray.length(); i++){
                try {
                    JSONObject lObject = lArray.getJSONObject(i);
                    TypeBrif lTypeBrif = new TypeBrif();
                    lTypeBrif.fromJson(lObject);

                    mTypeBrifs.add(lTypeBrif);
                }catch (JSONException pE){
                    pE.printStackTrace();
                }
            }
        }

        public ArrayList<Integer> getAllCata(){
            ArrayList<Integer> catas = new ArrayList<>();
            for (TypeBrif brif: mTypeBrifs) {
                catas.add(brif.type);
            }
            return catas;
        }

    }

    public class TypeBrif{
        public int type;
        public int amount;
        public String desc;

        public void fromJson(JSONObject pObject){
            if(pObject == null) return;

            type = pObject.optInt("cate", TQPublishType.TYPE_TB);

            amount = pObject.optInt("plan_amount");
            desc = pObject.optString("desc");
        }
    }

    private TQClassIntrAdapter.IClickListener mIClickListener = new TQClassIntrAdapter.IClickListener() {
        @Override
        public void subjectChange(int subject) {
        }

        @Override
        public void catChange(int cat) {

        }
    };
}
